import time
from datetime import datetime
import pymysql
from Case.python_ui.Common.basepage import Basepage
from Case.python_ui.PageLocate.product_management_locator import ProductManagementLocator
from Case.python_ui.PageLocate.user_oss_locator import UserOssLocator
from Case.python_ui.PageLocate.product_security_group_locator import SecurityGroupLocator


class  SecurityGroupPage(Basepage):
    global security_group_name
    now = datetime.now()
    current_date = now.strftime("%Y%m%d%H")
    security_group_name = "SG" + current_date

    def enter_ecs_console(self):
        doc = '进入ECS控制台'
        self.wait_elevisble(ProductManagementLocator.menu_list, doc)
        self.click_element(ProductManagementLocator.menu_list, doc)
        self.wait_elevisble(SecurityGroupLocator.product_list_ecs, doc)
        time.sleep(2)
        self.click_element(SecurityGroupLocator.product_list_ecs, doc)
        time.sleep(5)
        self.wait_elevisble(ProductManagementLocator.iframe, doc)
        self.driver.switch_to.frame(self.get_element(ProductManagementLocator.iframe, doc))
        time.sleep(5)
    def create_security_group(self):
        doc='创建安全组'
        self.wait_elevisble(SecurityGroupLocator.security_gp_tab, doc)
        time.sleep(3)
        self.click_element(SecurityGroupLocator.security_gp_tab, doc)
        time.sleep(2)
        self.wait_elevisble(SecurityGroupLocator.create_security_group, doc)
        time.sleep(3)
        self.click_element(SecurityGroupLocator.create_security_group, doc)
        time.sleep(2)
        self.wait_elevisble(SecurityGroupLocator.spName_input, doc)
        self.send_keys(SecurityGroupLocator.spName_input, security_group_name, doc)
        self.wait_elevisble(SecurityGroupLocator.submit_btn, doc)
        time.sleep(3)
        self.click_element(SecurityGroupLocator.submit_btn, doc)

    def search_security_group(self):
        doc ='搜索安全组'
        self.wait_elevisble(SecurityGroupLocator.security_gp_tab, doc)
        time.sleep(3)
        self.click_element(SecurityGroupLocator.security_gp_tab, doc)
        self.wait_elevisble(SecurityGroupLocator.search_ele, doc)
        time.sleep(2)
        self.click_element(SecurityGroupLocator.search_ele, doc)
        self.wait_elevisble(SecurityGroupLocator.select_by_name_ele, doc)
        time.sleep(2)
        self.click_element(SecurityGroupLocator.select_by_name_ele, doc)
        time.sleep(2)
        self.wait_elevisble(SecurityGroupLocator.search_by_name, doc)
        self.send_keys(SecurityGroupLocator.search_by_name, security_group_name, doc)
        time.sleep(3)
        self.wait_elevisble(SecurityGroupLocator.sg_one_ele, doc)
        time.sleep(1)
        search_result = self.get_element(SecurityGroupLocator.sg_one_ele, doc).text
        print(search_result)
        assert search_result == security_group_name


















